package com.hdyg.sixg.activity.home;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.InversTransRecordAdapter;
import com.hdyg.sixg.adapter.WalletAdapter;
import com.hdyg.sixg.entry.InvestBean;
import com.hdyg.sixg.entry.WalletBean;
import com.hdyg.sixg.mvp.Contrant.CGuquanWallet;
import com.hdyg.sixg.mvp.Contrant.CMiniWallet;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PGuquanWallet;
import com.hdyg.sixg.mvp.presenter.PMiniWallet;
import com.hdyg.sixg.ui.CustomLoadMoreView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 矿机钱包
 * @time 2019/4/9 10:42
 */

public class MiniWalletActivity extends BaseActivity implements CMiniWallet.IVMiniWallet {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_record)
    TextView tvRecord;
    @BindView(R.id.rv_record)
    RecyclerView rvRecord;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private final static int REQUEST_CODE = 0x00001;
    private WalletAdapter mAdapter;
    private CMiniWallet.IPMiniWallet mPresenter;
    private int page = 1,allDataSize;
    private List<WalletBean.ListBean> dataBeans;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_mini_wallet;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.main_mini_wallet);
        rvRecord.setLayoutManager(new LinearLayoutManager(mContext));
    }
    private void onClickListener(){
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            page = 1;
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());

    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.ROCK_RECORD_URL,GetParamUtil.getRockRecordParam(page));
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PMiniWallet(this);
    }

    private void initAdapter(){
        mAdapter = new WalletAdapter(R.layout.item_recharge_record,dataBeans);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        rvRecord.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(() -> {
            if (mAdapter.getData().size() < allDataSize){
                page++;
                initData();
            }else {
                mAdapter.loadMoreEnd();
            }
        },rvRecord);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK){
            return;
        }
        switch (requestCode) {
            case REQUEST_CODE:
                page = 1;
                initData();
                break;
        }
    }

    @Override
    public void vGetInitSuccess(WalletBean dataBean) {
        tvMoney.setText(dataBean.getBalance());
        dataBeans = dataBean.getList();
        allDataSize = Integer.valueOf(dataBean.getTotal());//总条数
        if (page == 1){
            if (mAdapter != null){
                mAdapter.setNewData(dataBeans);
            }else {
                if (dataBeans == null || dataBeans.size() == 0){
                    tvRecord.setVisibility(View.GONE);
                }else {
                    tvRecord.setVisibility(View.VISIBLE);
                }
                initAdapter();
            }
        }else {
            if (dataBeans.size() != 0){
                mAdapter.addData(dataBeans);
            }
            mAdapter.loadMoreComplete();
        }
    }
}
